<template>
  <a-card :bordered="false">
    <a-steps class="steps" :current="current">
      <a-step :title="$t('input')" />
      <a-step :title="$t('confirm')" />
      <a-step :title="$t('complete')" />
    </a-steps>
    <div class="content">
      <step1 v-if="current === 0" @nextStep="nextStep" @getProjectInfo="getProjectInfo"></step1>
      <step2 v-if="current === 1" @nextStep="nextStep" @prevStep="prevStep" @submit="submit" :project="project"></step2>
      <step3 v-if="current === 2" @prevStep="prevStep" @finish="finish"></step3>
    </div>
  </a-card>
</template>

<script>
import Step1 from './Step1'
import Step2 from './Step2'
import Step3 from './Step3'
import Axios from 'axios'
import {format} from 'date-fns'

export default {
  name: 'StepForm',
  i18n: require('./i18n'),
  components: {Step1, Step2, Step3},
  data () {
    return {
      url: this.$store.state.apiUrl + ':' + this.$store.state.apiPort,
      project: {
        projectName: "",
        projectTip: "",
        projectContent: "",
        projectReasion: "",
        projectClassId: 0,
        // 申报人与公司ID
        company_id: this.$route.params.company_id,
        company_user_id: this.$route.params.company_user_id,
        company: {},
        declarant: {},
      },
      current: 0
    }
  },
  computed: {
    desc() {
      return this.$t('pageDesc')
    }
  },
  methods: {
    nextStep () {
      console.log(this.project)
      if (this.current < 2) {
        this.current += 1
      }
    },
    prevStep () {
      if (this.current > 0) {
        this.current -= 1
      }
    },
    finish () {
      this.current = 0
    },
    getProjectInfo(project) {
      Object.assign(this.project, project)
    },
    // 添加项目
    submit() {
      console.log(this.company_user_id)
      Axios.post(this.url + '/pxManagerSystem/addProjectInfo', {
        "project_class_id": this.project.projectClassId,
        "project_name": this.project.projectName,
        "project_tip": this.project.projectTip,
        "project_content": this.project.projectContent,
        "company_id": this.project.company_id,
        "company_user_id": this.project.company_user_id,
        "project_reasion": this.project.projectReasion,
        "project_time": format(new Date(), "yyyy-MM-dd HH:mm:ss"),
        "project_check": 0,
      }).then(res => {
        console.log(res)
      }).catch(err => {
        console.log(err)
      })
    },
    // 获取申报人信息
    getUserInfo() {
      Axios.post(this.url + '/pxManagerSystem/getUserByUserId', {
        user_id: this.project.company_user_id
      }).then(res => {
        console.log(res)
        this.project.declarant = res.data.data?.userInfolist[0]
      }).catch(err => {
        console.log(err)
      })
    },
    // 获取公司信息
    getCompanyInfo() {
      Axios.post(this.url + '/pxManagerSystem/getCompanyInfoByCompanyInfoId', {
        company_id: this.project.company_id
      }).then(res => {
        console.log(res)
        this.project.company = res.data.data?.companyInfoList[0]
      }).catch(err => {
        console.log(err)
      })
    }
  },
  mounted() {
    this.getCompanyInfo()
    this.getUserInfo()
  }
}
</script>

<style lang="less" scoped>
.steps{
  max-width: 950px;
  margin: 16px auto;
}
</style>
